﻿using System;
using System.Collections.Generic;
using DataStore;
using DataStore.Domain;
using DataStore.Repositories;
using Infrastructure.CurrentData;
using Microsoft.Practices.Composite.Events;

namespace Infrastructure.ViewModels
{
    /// <summary>
    /// Person viewmodel
    /// </summary>
    public class PersonViewModel : DataModelSingle<Person>
    {
        /// <summary>
        /// Occurs when [updated].
        /// </summary>
        public event EventHandler<EventArgs> Updated;
        /// <summary>
        /// Called when [updated].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnUpdated(object sender, EventArgs e)
        {
            if(Updated != null)
            {
                Updated(sender, e);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PersonViewModel"/> class.
        /// </summary>
        /// <param name="person">The person.</param>
        public PersonViewModel(Person person) : base(person)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="PersonViewModel"/> class.
        /// </summary>
        /// <param name="eventAggregator">The event aggregator.</param>
        /// <param name="person">The person.</param>
        public PersonViewModel(IEventAggregator eventAggregator, Person person) : base(eventAggregator, person)
        { }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public virtual Guid Id
        {
            get
            {
                return Item.Id;
            } 
            set
            {
                Item.Id = value;
                OnPropertyChanged("Id");
            }
        }
        /// <summary>
        /// Gets or sets the full name of the person.
        /// </summary>
        /// <value>The full name.</value>
        public virtual string Name
        {
            get
            {
                return string.Format("{0} {1} {2}", Title, FirstName, LastName);
            }
        }
        /// <summary>
        /// Gets or sets the first name.
        /// </summary>
        /// <value>The first name.</value>
        public virtual string FirstName
        {
            get
            {
                return Item.FirstName;
            }
            set
            {
                Item.FirstName = value;
                OnPropertyChanged("FirstName");
            }
        }
        /// <summary>
        /// Gets or sets the name of the middle.
        /// </summary>
        /// <value>The name of the middle.</value>
        public virtual string MiddleName
        {
            get
            {
                return Item.MiddleName;
            }
            set
            {
                Item.MiddleName = value;
                OnPropertyChanged("MiddleName");
            }
        }
        /// <summary>
        /// Gets or sets the last name.
        /// </summary>
        /// <value>The last name.</value>
        public virtual string LastName
        {
            get
            {
                return Item.LastName;
            }
            set
            {
                Item.LastName = value;
                OnPropertyChanged("LastName");
            }
        }
        /// <summary>
        /// Gets or sets the institution.
        /// </summary>
        /// <value>The institution.</value>
        public virtual ICollection<Company> Institutions { get; set; }
        /// <summary>
        /// Gets or sets the emails.
        /// </summary>
        /// <value>The emails.</value>
        public virtual ICollection<EmailLink> Emails { get; set; }
        /// <summary>
        /// Gets or sets the websites.
        /// </summary>
        /// <value>The websites.</value>
        public virtual ICollection<WebPageLink> Websites { get; set; }
        /// <summary>
        /// Gets or sets the images made about the person.
        /// </summary>
        /// <value>The images.</value>
        public virtual ICollection<ImageLink> Images { get; set; }
        /// <summary>
        /// Gets or sets the phone numbers.
        /// </summary>
        /// <value>The phone numbers.</value>
        public virtual ICollection<string> PhoneNumbers { get; set; }
        /// <summary>
        /// Gets or sets the addresses.
        /// </summary>
        /// <value>The addresses.</value>
        public virtual ICollection<Address> Addresses { get; set; }
        /// <summary>
        /// Gets or sets the comment.
        /// </summary>
        /// <value>The comment.</value>
        public virtual string Comment { get; set; }
        /// <summary>
        /// Gets or sets the employment position, job.
        /// </summary>
        /// <value>The position.</value>
        public virtual string Position
        {
            get
            {
                return Item.Position;
            }
            set
            {
                Item.Position = value;
                OnPropertyChanged("Position");
            }
        }
        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public virtual string Title
        {
            get
            {
                return Item.Title;
            }
            set
            {
                Item.Title = value;
                OnPropertyChanged("Title");
            }
        }

        /// <summary>
        /// Gets a value indicating whether thye entity is a new item.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the entity is a new item; otherwise, <c>false</c>.
        /// </value>
        protected override bool IsNewItem
        {
            get
            {
                return Item.Id == Guid.Empty;
            }
        }

        /// <summary>
        /// Saves the entity.
        /// </summary>
        public override void Save()
        {
            IDatabase database = Current.Container.Resolve<IDatabase>();
            using (var unitOfWork = database.CreateUnitOfWork())
            {

                try
                {
                    IRepository<Person> repository = new PersonRepository(unitOfWork);
                    unitOfWork.BeginTransaction();

                    repository.SaveOrUpdate(Item);

                    unitOfWork.CommitTransaction();
                }
                catch
                {
                    unitOfWork.RollbackTransaction();
                    throw;
                }
            }


            OnUpdated(this, EventArgs.Empty);
            base.Save();
        }

        protected override bool CanSave
        {
            get
            {
                return true;
            }
        }
    }
}